<?php

class Ht_Plugin_Navigation extends Zend_Controller_Plugin_Abstract
{
	
	protected function _renderMainNav(Zend_Controller_Request_Abstract $request)
	{
		//Navigation nur ausgeben, wenn der User eingeloggt ist
		if(!Zend_Auth::getInstance()->hasIdentity()) return '<ul><li><a href="/'.Zend_Registry::get('Zend_Locale').'/auth/">Login</a></li></ul>';
		//Objekt für Rechtemanagement
		$acl = new Model_Acl();
		//Objekt für den aktuellen User
		$user = Zend_Auth::getInstance()->getIdentity();
		//Navigation als <ul>
		$nav = '';
		$nav .= '<ul>';
		$nav .= '<li><a href="/'.Zend_Registry::get('Zend_Locale').'/">Start</a></li>';
		$nav .= '<li><a href="/'.Zend_Registry::get('Zend_Locale').'/tickets/">Tickets</a></li>';
		$nav .= '<li><a href="/'.Zend_Registry::get('Zend_Locale').'/projects/">Projekte</a></li>';
		if($acl->isAllowed($user->rid, 'users')) {
			$nav .= '<li><a href="/'.Zend_Registry::get('Zend_Locale').'/users/">Kunden & Benutzer</a></li>';
		}
		if($acl->isAllowed($user->rid, 'invoices')) {
			$nav .= '<li><a href="/'.Zend_Registry::get('Zend_Locale').'/invoices/">Rechnungen</a></li>';
		}
		$nav .= '<li><a href="/'.Zend_Registry::get('Zend_Locale').'/account/">Account</a></li>';
		$nav .= '</ul>';
		return $nav;
	}
	
	protected function _renderSubNav(Zend_Controller_Request_Abstract $request)
	{
		//Navigation nur ausgeben, wenn der User eingeloggt ist
		if(!Zend_Auth::getInstance()->hasIdentity()) return '<ul></ul>';
		//Navigation als <ul>
		$nav = '';
		$nav .= '<ul>';
		$nav .= '<li><a href="/'.Zend_Registry::get('Zend_Locale').'/'.$request->getControllerName().'/">Übersicht</a></li>';
		//Für alle Controller ausser dem Index-Controller
		if($request->getControllerName() != "index"
			&& $request->getControllerName() != "error"	 
			&& $request->getControllerName() != "invoices"
			 && $request->getControllerName() != "account") {
			$nav .= '<li><a href="/'.Zend_Registry::get('Zend_Locale').'/'.$request->getControllerName().'/new">Neu erstellen</a></li>';
		}
		if($request->getControllerName() == "invoices") {
			$nav .= '<li><a href="/'.Zend_Registry::get('Zend_Locale').'/'.$request->getControllerName().'/completedtickets">Tickets verrechnen</a></li>';
			$nav .= '<li><a href="/cron.php">Cronjob manuell ausführen</a></li>';
		}
		$nav .= '</ul>';
		return $nav;
	}
	
	public function postDispatch(Zend_Controller_Request_Abstract $request)
	{
		Zend_Layout::getMvcInstance()->main_navigation = $this->_renderMainNav($request);
		Zend_Layout::getMvcInstance()->sub_navigation = $this->_renderSubNav($request);
	}
};
?>